﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Contract
{
    public class CurrencyContractAppService : ApplicationService, ICurrencyContractAppService
    {
        private readonly ICurrencyContractRepository _currency;
        private readonly IRepository<Currency, int> _currencyRepository;
        private readonly LogisticsDbContext db;
        public CurrencyContractAppService(ICurrencyContractRepository currency, IRepository<Currency, int> currencyRepository, LogisticsDbContext dbc)
        {
            _currency = currency;
            _currencyRepository = currencyRepository;
            db = dbc;
        }
        public async Task<int> Create(CurrencyDto cu)
        {
            var carr = new Currency
            {
                NumberId = cu.NumberId,
                ContractTitle = cu.ContractTitle,
                OppositeUnit = cu.OppositeUnit,
                OtherPartyPerson = cu.OtherPartyPerson,
                Line = cu.Line,
                Freightrate = cu.Freightrate,
                CharterConditions = cu.CharterConditions,
                CharterAmount = cu.CharterAmount,
                SigningDate = Convert.ToDateTime(cu.SigningDate),
                Handler = cu.Handler,
                ProjectDescription = cu.ProjectDescription,
                ChangeClause = cu.ChangeClause,
                Enclosure = cu.Enclosure,
                CreationTime=cu.CreationTime,
            };

            int result= await _currency.Create(carr);
            return result;
        }

        public async Task<int> Delete(int Id)
        {
            var result = await _currency.Delete(Id);
            return result;
        }

        public async Task<CurrencyDto> GetCurrencyById(int Id)
        {
            var student = await _currency.GetCurrencyById(Id);
            return ObjectMapper.Map<Currency, CurrencyDto>(student);
        }

        public async Task<PagedResultDto<CurrencyDto>> GetCurrencys(CurrencySearchDto csd)
        {
            var carryList = await _currencyRepository.GetPagedListAsync(
                csd.SkipCount,
                csd.MaxResultCount,
                csd.Sorting);
            if (!string.IsNullOrEmpty(csd.Title))
            {
                carryList.Where(m => m.ContractTitle.Contains(csd.Title)).ToList();
            }
            if (!string.IsNullOrEmpty(csd.Time))
            {
                carryList.Where(m => m.CreationTime == (Convert.ToDateTime(csd.Time))).ToList();
            }
            var totalCount = await AsyncExecuter.CountAsync(
                 _currencyRepository.WhereIf(
                     !csd.Title.IsNullOrWhiteSpace(),
                     m => m.ContractTitle.Contains(csd.Title)
                 )
             );

            return new PagedResultDto<CurrencyDto>(
              totalCount,
              ObjectMapper.Map<List<Currency>, List<CurrencyDto>>(carryList)
          );
        }

        public async Task<PagedResultDto<CurrencyDto>> GetDapperCurrencys(CurrencySearchDto csd)
        {
            var carr = db.Currency.ToList();
            var file = db.Field.ToList();
            var lin = db.Line.ToList();
            var own = db.Owner.ToList();
            var list = from a in carr
                       join b in own on a.OtherPartyPerson equals b.Id
                       join c in file on a.OppositeUnit equals c.Id
                       join d in lin on a.Line equals d.Id
                       select new CurrencyDto
                       {
                           Id = a.Id,
                           NumberId = a.NumberId,
                           ContractTitle = a.ContractTitle,
                           OppositeUnit = a.OppositeUnit,
                           OtherPartyPerson = a.OtherPartyPerson,
                           Line = a.Line,
                           Freightrate = a.Freightrate,
                           CharterConditions = a.CharterConditions,
                           CharterAmount = a.CharterAmount,
                           SigningDate = a.SigningDate,
                           Handler = a.Handler,
                           State = a.State,
                           ProjectDescription = a.ProjectDescription,
                           ChangeClause = a.ChangeClause,
                           Enclosure = a.Enclosure,
                           CreationTime = a.CreationTime,
                           OName = b.Name,
                           FName = c.Name,
                           LName = d.Name,
                       };
            if (!string.IsNullOrEmpty(csd.Title))
            {
                list = list.Where(m => m.ContractTitle.Contains(csd.Title));
            }
            var totalCount = list.Count();
            list = list.Skip(csd.SkipCount).Take(csd.MaxResultCount);
            return new PagedResultDto<CurrencyDto>(
              totalCount,
              list.ToList()
          );
        }

        public async Task<int> Update(CurrencyDto cu)
        {
            var carr = await _currencyRepository.GetAsync(cu.Id);
            carr.ContractTitle = cu.ContractTitle;
            carr.OppositeUnit = cu.OppositeUnit;
            carr.OtherPartyPerson = cu.OtherPartyPerson;
            carr.Line = cu.Line;
            carr.Freightrate = cu.Freightrate;
            carr.CharterConditions = cu.CharterConditions;
            carr.CharterAmount = cu.CharterAmount;
            carr.SigningDate = cu.SigningDate;
            carr.Handler = cu.Handler;
            carr.ProjectDescription = cu.ProjectDescription;
            carr.ChangeClause = cu.ChangeClause;
            carr.Enclosure = cu.Enclosure;
            await _currencyRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
